package com.bill.concurrency.config;

import com.bill.concurrency.filters.HttpFilter;
import com.bill.concurrency.listeners.DemoListener;
import com.bill.concurrency.servlets.DemoServlet;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author bill
 */
@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    /**
     * 通过代码注册一个filter
     * @return
     */
    @Bean
    public FilterRegistrationBean httpFilter(){
        HttpFilter httpFilter=new HttpFilter();
        FilterRegistrationBean registrationBean=new FilterRegistrationBean();
        registrationBean.setFilter(httpFilter);
        List<String> urlPatterns= Lists.newArrayList();
        urlPatterns.add("/*");
        registrationBean.setUrlPatterns(urlPatterns);
        registrationBean.setOrder(1);
        return registrationBean;
    }

    /**
     * 通过代码注册一个servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean demoServlet(){
        DemoServlet demoServlet=new DemoServlet();
        ServletRegistrationBean registrationBean=new ServletRegistrationBean();
        registrationBean.setServlet(demoServlet);
        List<String> urlMappings=Lists.newArrayList();
        urlMappings.add("/demo");
        registrationBean.setUrlMappings(urlMappings);
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }

    /**
     * 通过代码注册一个servletListener
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean demoListener(){
        ServletListenerRegistrationBean registrationBean=new ServletListenerRegistrationBean();
        DemoListener demoListener =new DemoListener();
        registrationBean.setListener(demoListener);
        return registrationBean;
    }
}
